package com.sdau.zhang.controller;

import com.sdau.zhang.dao.OrderDao;
import com.sdau.zhang.service.IProductService;
import com.sdau.zhang.vo.Order;
import com.sdau.zhang.vo.Product;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/show")
public class ShowController {

    @Autowired
    IProductService productService;

    @Autowired
    OrderDao orderDao;

    @RequestMapping("/enter")
    public String enter(Model model, int id, HttpSession session)
    {
        Product product = productService.queryById(id);
        int userid = 1;//session.getAttribute("userid");
        model.addAttribute("product",product);
        return "Mr_z/show.html";
    }
    @RequestMapping("/detail")
    public String detail(Model model,int pid)
    {
        Product product = productService.queryById(pid);
        model.addAttribute("product",product);
        return "Mr_z/show/detail.html";
    }
    @RequestMapping("/comment")
    public String comment(Model model,int pid)
    {
        Product product = productService.queryById(pid);
        model.addAttribute("product",product);
        return "Mr_z/show/comment.html";
    }
    @RequestMapping("/saleNote")
    public String saleNote(Model model,int pid)
    {
        Product product = productService.queryById(pid);
        List<Order> lo = orderDao.queryByPId(pid);
        model.addAttribute("product",product);
        model.addAttribute("orderList",lo);
        for(Order order:lo)
        {
            System.out.println(order.getPtime());
        }
        return "Mr_z/show/saleNote.html";
    }
    @RequestMapping("/consult")
    public String consult(Model model,int pid)
    {
        Product product = productService.queryById(pid);
        model.addAttribute("product",product);
        return "Mr_z/show/consult.html";
    }
    @RequestMapping(value="/shoppingCart",method = RequestMethod.POST)
    public @ResponseBody String shoppingCart(@RequestBody String order)
    {
        JSONObject jsonObject = JSONObject.fromObject(order);
        int num = Integer.parseInt((String)jsonObject.get("num"));
        int pid = Integer.parseInt((String)jsonObject.get("pid"));
        String veget = productService.getVegetById(pid);
        String msg = num+"斤"+veget+"已放入您的购物车";

        return msg;
    }
    @RequestMapping(value="/purchase",method=RequestMethod.POST)
    public @ResponseBody String purchase(@RequestBody String order)
    {
        System.out.println(order);
        JSONObject jsonObject = JSONObject.fromObject(order);
        jsonObject.put("oid",null);
        for(Object key : jsonObject.keySet()){
            System.out.println("key:"+(String)key);
            Object value = jsonObject.get(key);
            System.out.println("value:"+value);
        }
        double sum = (Double) jsonObject.get("sum");
        Order ord = (Order) JSONObject.toBean(jsonObject,Order.class);
        String msg="购买成功，本次共消费"+sum;

        orderDao.createOrder(ord);
        return msg;
    }
    @CrossOrigin
    @RequestMapping(value="/all",method = RequestMethod.GET)
    public @ResponseBody List queryAll()
    {
        return productService.queryAll();
    }

}
